import config from '@/config'
import storage from '@/utils/storage'
import constant from '@/utils/constant'
import {login, logout, getInfo} from '@/api/login'
import {getToken, setToken, removeToken} from '@/utils/auth'
import {changeUser} from "../../api/login";
import {updateSession} from "../../api/userInfo";

const baseUrl = config.baseUrl

const user = {
    state: {
        token: getToken(),
        name: storage.get(constant.name),
        mobile: storage.get(constant.mobile),
        staffId: storage.get(constant.staffId),
        avatar: storage.get(constant.avatar),
        roles: storage.get(constant.roles),
        staff: storage.get(constant.staff),
        isCompany: storage.get(constant.isCompany),
        companyId: storage.get(constant.companyId),
        companyInfo: storage.get(constant.companyInfo),
        permissions: storage.get(constant.permissions),
    },
    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_STAFF_ID: (state, staffId) => {
            state.staffId = staffId
            storage.set(constant.staffId, staffId)
        },
        SET_MOBILE: (state, mobile) => {
            state.mobile = mobile
            storage.set(constant.mobile, mobile)
        },
        SET_NAME: (state, name) => {
            state.name = name
            storage.set(constant.name, name)
        },
        SET_USER: (state, user) => {
            state.staff = user
            storage.set(constant.staff, user)
        },
        SET_COMPANY_ID: (state, companyId) => {
            state.companyId = companyId
            storage.set(constant.companyId, companyId)
        },
        SET_COMPANY_INFO: (state, companyInfo) => {
            state.companyInfo = companyInfo
            storage.set(constant.companyInfo, companyInfo)
        },
        SET_IS_COMPANY: (state, isCompany) => {
            state.isCompany = isCompany
            storage.set(constant.isCompany, isCompany)
        },
        SET_AVATAR: (state, avatar) => {
            state.avatar = avatar
            storage.set(constant.avatar, avatar)
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles
            storage.set(constant.roles, roles)
        },
        SET_PERMISSIONS: (state, permissions) => {
            state.permissions = permissions
            storage.set(constant.permissions, permissions)
        },
    },

    actions: {
        // 登录
        Login({commit}, userInfo) {
            const mobile = userInfo.mobile;
            const password = userInfo.password
            const smscode = userInfo.smscode
            const type = userInfo.type
            return new Promise((resolve, reject) => {
                login(mobile, password, smscode, type).then(res => {
                    setToken(res.token)
                    commit('SET_TOKEN', res.token)
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 获取用户信息
        GetInfo({commit, state}) {
            return new Promise((resolve, reject) => {
                getInfo().then(res => {
                    const user = res.user
                    const avatar = (user == null || user.avatar == "" || user.avatar == null) ? require("@/static/images/personal-avatar.png") : baseUrl + user.avatar
                    const username = (user == null || user.name == "" || user.name == null) ? "" : user.name
                    if (res.roles && res.roles.length > 0) {
                        commit('SET_ROLES', res.roles)
                        commit('SET_PERMISSIONS', res.permissions)
                    } else {
                        commit('SET_ROLES', ['ROLE_DEFAULT'])
                    }
                    commit('SET_COMPANY_ID', user.companyId)
                    // 当前切换公司信息
                    if (user.companyId != 0) {
                        commit('SET_COMPANY_INFO', user.companys.filter((e) => e.id == user.companyId)[0])
                        commit('SET_IS_COMPANY', true)

                    } else {
                        commit('SET_IS_COMPANY', false)
                    }
                    commit('SET_USER', user)
                    commit('SET_STAFF_ID', user.id)
                    commit('SET_MOBILE', user.mobile)
                    commit('SET_NAME', username)
                    commit('SET_AVATAR', avatar)
                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })
        },
        // 设置用户信息
        SetStaffInfo({commit, state}) {
            return new Promise((resolve, reject) => {
                commit('SET_USER', state)
            })
        },

        //切换企业/个人
        SwitchCompany({commit, state}, companyId) {
            return new Promise((resolve, reject) => {
                commit('SET_COMPANY_ID', companyId);
                changeUser(companyId).then(() => {
                    updateSession().then((response) => {
                        commit('SET_USER', response.data)
                        resolve(response.data)
                    });
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 退出系统
        LogOut({commit, state}) {
            return new Promise((resolve, reject) => {
                logout(state.token).then(() => {
                    commit('SET_TOKEN', '')
                    commit('SET_ROLES', [])
                    commit('SET_PERMISSIONS', [])
                    removeToken()
                    uni.removeStorageSync("noSetPasswordForPserson");
                    uni.removeStorageSync("wechatAuthCode");
                    storage.clean()
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        }
    }
}

export default user
